//
//  UIImage+MJ.m
//  新浪微博
//
//  Created by apple on 13-10-26.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "UIImage+Extension.h"


@implementation UIImage (Extension)

#pragma mark - 根据色值返回一个图片
+ (UIImage *)imageWithColor:(UIColor *)color withSize:(CGSize)size
{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    //开启基于位图的上下文
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //画一个color颜色的矩形框
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    //拿到图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    UIGraphicsEndImageContext();
    return image;
}
#pragma mark 可以自由拉伸的图片
+ (UIImage *)resizedImage:(NSString *)imgName
{
    return [self resizedImage:imgName xPos:0.5 yPos:0.5];
}

+ (UIImage *)resizedImage:(NSString *)imgName xPos:(CGFloat)xPos yPos:(CGFloat)yPos
{
    UIImage *image = [UIImage imageNamed:imgName];
    return [image stretchableImageWithLeftCapWidth:image.size.width * xPos topCapHeight:image.size.height * yPos];
}
#pragma mark - 返回一个指定大小的Image
+ (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    
    if(img == nil)  return img;
    
    // 创建一个bitmap的context
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}
#pragma mark - 圆形图片
- (UIImage *)circleImageWithSize:(CGSize)size
{
    // NO代表透明
    UIGraphicsBeginImageContextWithOptions(size, NO, 2.0);
    // 获得上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 添加一个圆
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    CGContextAddEllipseInRect(ctx, rect);
    // 裁剪
    CGContextClip(ctx);
    // 将图片画上去
    [self drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
#pragma mark - 缩略图
- (UIImage *)getThumbImage
{
    CGSize size = self.size;
    double max = size.width > size.height ? size.width : size.height;
    UIImage *thumbImage = self;
    if (max > 100){
        double scale = 100 / max;
        thumbImage = [self scaleToSizeImage:CGSizeMake(size.width * scale, size.height * scale)];
    }
    return thumbImage;
}
//缩放图片
- (UIImage *)scaleToSizeImage:(CGSize)size
{
    CGRect rect = CGRectIntegral(CGRectMake(0,0,size.width,size.height));
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 2.0);
    [self drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

#pragma mark - 生成二维码CIImage对象
+ (CIImage *)generateQRCodeImage:(NSString *)source
{
    if (source.length == 0) return nil;
    NSData *data = [source dataUsingEncoding:NSUTF8StringEncoding];
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    //设置过滤器默认属性
    [filter setDefaults];
    [filter setValue:data forKey:@"inputMessage"];
    [filter setValue:@"Q" forKey:@"inputCorrectionLevel"];
    
    return filter.outputImage;
}
#pragma mark - 生成二维码Image对象
+ (UIImage *)generateQRCodeImage:(NSString *)source size:(CGSize)size
{
    CIImage *ciimage = [self generateQRCodeImage:source];
    if (ciimage) {
        CGRect extent = CGRectIntegral(ciimage.extent);
        CGFloat scaleWidth = size.width/CGRectGetWidth(extent);
        CGFloat scaleHeight = size.height/CGRectGetHeight(extent);
        size_t width = CGRectGetWidth(extent) * scaleWidth;
        size_t height = CGRectGetHeight(extent) * scaleHeight;
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
        CGContextRef contentRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpaceRef, (CGBitmapInfo)kCGImageAlphaNone);
        CIContext *context = [CIContext contextWithOptions:nil];
        CGImageRef imageRef = [context createCGImage:ciimage fromRect:extent];
        CGContextSetInterpolationQuality(contentRef, kCGInterpolationNone);
        CGContextScaleCTM(contentRef, scaleWidth, scaleHeight);
        CGContextDrawImage(contentRef, extent, imageRef);
        CGImageRef imageRefResized = CGBitmapContextCreateImage(contentRef);
        CGContextRelease(contentRef);
        CGImageRelease(imageRef);
        return [UIImage imageWithCGImage:imageRefResized];
    }else{
        return nil;
    }
}
#pragma mark - 返回一个指定大小的data 
+(NSData *)compressWithImage:(UIImage *)image toKb:(NSInteger)kb
{
    kb *= 1024;
    CGFloat compression = 1.0f;
    CGFloat maxCompression = 0.1f;
    NSData *imageData = UIImageJPEGRepresentation(image, compression);
    while ([imageData length] > kb && compression > maxCompression) {
        compression -= 0.1;
        imageData = UIImageJPEGRepresentation(image, compression);
    }
    return imageData;
}
@end
